#Copyright (c) 2025, Alibaba Cloud and its affiliates;
#Licensed under the Apache License, Version 2.0 (the "License");
#you may not use this file except in compliance with the License.
#You may obtain a copy of the License at

#   http://www.apache.org/licenses/LICENSE-2.0

#Unless required by applicable law or agreed to in writing, software
#distributed under the License is distributed on an "AS IS" BASIS,
#WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#See the License for the specific language governing permissions and
#limitations under the License.



import json

from fce_util.libs.log import g_logger
import fce_util.deploys.resource_mgr as resource

logger = g_logger.logger

def cluster_delete(args):
    namespace = args.namespace if args.namespace else 'default'

    logger.debug('cluster_create enter,args:{}'.format(args))
    if resource.checkServiceExist('rc', args.cluster_name, namespace):
        resource.clean('rc', args.cluster_name, namespace)
    else:
        logger.warning('Ray cluster {} resource not exist, please check'.format(args.cluster_name))

    msg = {'Success': True}
    msg['Message'] = {
        'Msg': 'Kube ray cluster resource clean success'
    }
    print(json.dumps(msg))

def FCE_cluster_delete(args):
    if args.rayc == 'cluster_delete':
        cluster_delete(args)
        return
    args.this_parser.print_help()

def make(parser):
    """
    FCE Ray Cluster Delete entry
    """
    parser.add_argument( 
        '--cluster_name',
        dest='cluster_name',
        nargs = '?',
        metavar = ('CLUSTER_NAME'),
        help='Ray Cluster Name',
        )
    
    parser.add_argument( 
        '--cluster_id',
        dest='cluster_id',
        nargs = '?',
        metavar = ('CLUSTER_ID'),
        help='Ray Cluster Id',
        )   
    
    parser.add_argument( 
        '--namespace',
        dest='namespace',
        nargs = '?',
        metavar = ('NAMESPACE'),
        help='Ray Cluster Namespace',
        )
    
    parser.add_argument( 
        '--kube_config',
        dest='kube_config',
        nargs = '?',
        metavar = ('KUBE_CONFIG'),
        help='Kube Config File Path',
        required=False,
    )

    parser.set_defaults(
        func=FCE_cluster_delete,
        this_parser=parser
    )